﻿using Framework.Domain.Enum;
using Framework.Domain.Model;
using Framework.Infrastructure;
using Framework.Infrastructure.RabbitMQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Framework.MVC.Infrastructure.Filters
{
    /// <summary>
    /// 手机端操作日志过滤器
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class PhoneSystemLogAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 分类
        /// </summary>
        public CategoryEnum Category { get; set; }

        /// <summary>
        /// 控制器
        /// </summary>
        public ActionEnum Action { get; set; }

        /// <summary>
        /// 操作
        /// </summary>
        public OperateEnum Operate { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string token = HttpContext.Current.Request["token"];
            var model = BaseController.GetPhoneTokenModelByToken(token);

            if (model != null)
            {
                SYS_OperationLog operationLog = new SYS_OperationLog();
                operationLog.UserId = model.Guid;
                operationLog.UserIp = Common.GetIp();
                operationLog.Category = Category;
                operationLog.Action = Action;
                operationLog.Operate = Operate;
                operationLog.Api = filterContext.RouteData.Values["controller"].ToString() + "/" + filterContext.RouteData.Values["action"].ToString();
                operationLog.Params = Common.GetPostParas();

                //RabbitMQ消息发布
                RabbitPublisher.Publish(new RabbitModel_Base { ModelType = RabbitModel_Enum.OperationLog, OperationLog = operationLog });
            }
        }

    }
}